<?php
namespace Admin\Controller;
/**
 * 财务有管理    --  有效订单
 * User: linkwhy
 * Date: 2015/10/29
 * Time: 23:43
 */
class FinanceController extends AdminController{

    public function index(){
        $item = M('order_item');
        $time_start = I('get.time-start');
        $time_end = I('get.time-end');
        $timetypeval = I('get.timetypeval', 0);
        $show_type = I('get.show_type');
        if( $timetypeval < 0 ){
            $timetypeval = 0;
        }

        $where['order_status'] = 6;
        $where['take_status'] = 2;
        $where['is_clear'] = 1;

        if ( !empty($time_start) ) {
            if( $timetypeval == 0 ){
                $where['pay_time'][] = array('egt',strtotime($time_start));
            } else {
                $where['success_time'][] = array('egt',strtotime($time_start));
            }
        }
        if ( !empty($time_end) ) {
            if( $timetypeval == 0 ){
                $where['pay_time'][] = array('elt',24*60*60 + strtotime($time_end));
            }else{
                $where['success_time'][] = array('elt',24*60*60 + strtotime($time_end));
            }
        }

        if(!empty($show_type)){
            $this->downExcel($where);
        }

        $list_order   = $this->lists('order', $where);
        int_to_string($list_order);
        $list_order = $this->getArray($list_order);
        $this->assign('_list', $list_order);
        $this->display();
    }

    public function downExcel($where = array()){
        $xlsName  = "Order-" . date('Y-m-d', time());

        $xlsCell  = array(
            array('key','序号'),
            array('order_id','订单号'),
            array('pay_time','支付时间'),
            array('success_time','完成时间'),
            array('pock_money','消费总金额'),
            array('bank','银行手续费'),
            array('zhekouAll','折扣'),
            array('payment','货款'),
            array('jj_pariceAll','总进价'),
            array('profit','利润'),
        );
        $list_order = M('order')->where($where)->select();
        $list_order = $this->getArray($list_order);

        foreach($list_order as $key => &$val){
            $val['key'] = $key + 1;
            $val['pay_time'] = date('Y-m-d H:i:s', $val['pay_time']);
            $val['success_time'] = date('Y-m-d H:i:s', $val['success_time']);
        }

        $this->exportExcel($xlsName,$xlsCell,$list_order);
    }

    private function getArray($list_order){
        $item = M('order_item');
        $list_item = array();
        $jj_priceAll = 0;
        $zhekouAll = 0;
        $bank = 0;
        foreach( $list_order as $key => &$val ){
            $list_item = $item->alias('item')->field('goods_id,num,jj_price,zhekou')->join('__CART__ c ON item.cart_id = c.id')->join('__GOODS__ g ON c.goods_id = g.id')->where(array('order_id' => $val['order_id']))->find();
            $val['jj_pariceAll'] = floatval($list_item['jj_price']) * intval($list_item['num']);
            $val['zhekouAll'] = floatval($val['pock_money']) * intval($list_item['zhekou'])/100;
            $bank = floatval($val['pock_money']) * C('BRANKFEE');                   //计算银行手续费
            $val['bank'] = $bank;
            $val['payment'] = floatval($val['pock_money']) - $val['bank'] - $val['zhekouAll'];    //货款
            $val['profit'] = floatval($val['payment']) - $val['jj_pariceAll'];                    //利润
            unset($list_item);
        }
        return $list_order;
    }

}